<template>
  <div>
    <el-dialog
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title="设置训练员"
      :visible="open"
      width="500px"
      append-to-body
      @close="cancel()"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="训练员" prop="ids">
          <el-select
            v-model="form.ids"
            filterable
            multiple
            placeholder="请选择"
            style="width: 95%"
          >
            <el-option
              v-for="dict in users"
              :key="dict.userId"
              :label="dict.name"
              :value="dict.userId"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end"
      >
        <el-button v-preventReClick @click="cancel">取 消</el-button>
        <el-button v-preventReClick type="primary" @click="submitForm"
          >添 加</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import {
  getMineSchoolIds,
  setTrainer,
  getSettedTrainer,
} from "@/api/course/course";
import { listPost_student } from "@/api/schoolOrganization/student";

export default {
  props: {
    open: Boolean,
    courseId: [Number, String],
    orderId: [Number, String],
    schoolIds: [Number, String],
  },
  data() {
    return {
      // 表单参数
      form: {
        ids: undefined,
      },
      // 表单校验
      rules: {
        // ids: [{ required: true, message: "训练员不能为空", trigger: "blur" }],
      },
      users: [],
    };
  },
  watch: {
    open(e) {
      if (e) {
        this.reset();
        // 获取人员列表 and 原本就有勾选的训练员
        this.FNgetMineSchoolIds();
      }
    },
  },
  mounted() {},
  methods: {
    /* 获取人员列表 and 原本就有勾选的训练员 */
    async FNgetMineSchoolIds() {
      // 先获取门店 在通过门店来获取人员
      // let res = await getMineSchoolIds();
      // let MineSchoolIds = res.data;
      // 获取人员列表
      let response = await listPost_student({
        schoolIds: this.schoolIds,
        status: 0,
        pageNum: 1,
        pageSize: 100000,
      });
      this.users = response.rows;
      // 在获取已存在的训练员
      console.log("在获取已存在的训练员", {
        id: this.courseId,
        orderId: this.orderId,
      });
      let res2 = await getSettedTrainer({
        id: this.courseId,
        orderId: this.orderId,
      });
      this.form.ids = res2.data.split(",").map((item) => {
        return Number(item);
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          // 设置训练员接口
          let res = await setTrainer({
            id: this.courseId,
            teacherIds: this.form.ids.join(","),
            orderId: this.orderId,
          });
          this.$emit("update:open", false);
          this.$emit("refresh", true);
          this.$modal.msgSuccess("操作成功");
        }
      });
    },
    // 表单重置
    reset() {
      this.form = {
        ids: undefined,
      };
      this.resetForm("form");
    },
    cancel() {
      this.$emit("update:open", false);
    },
  },
};
</script>
  <style scoped lang="scss"></style>
  